﻿using System;
using System.BLL;
using System.Collections.Generic;
using System.Dto;
using System.IBLL;
using System.Linq;
using System.MVC.Models;
using System.Web;
using System.Web.Mvc;

namespace System.MVC.Controllers
{
    /// <summary>
    /// 停车场管理控制器
    /// </summary>
    public class CarParkController : Controller
    {
        /// <summary>
        /// 车辆信息管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult ParkingPlaceList()
        {
            IParkingManager manager = new ParkingManager();
            var data = manager.GetParkingSpaceNumberLists();
            ViewBag.data = data;
            return View();
        }
        /// <summary>
        /// 车辆信息管理页面 table数据请求接口
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="Id">车库信息Id筛选</param>
        /// <param name="parkinglLocation">车库位置筛选</param>
        /// <returns></returns>
        public JsonResult GetParkingPlaceList(int limit, int page, string Id, string parkinglLocation)
        {
            IParkingManager manager = new ParkingManager();
            int datalet;
            var data = manager.GetparkingLotListDtos(limit, page, out datalet, Id, parkinglLocation);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加车位信息页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddParkingPlace()
        {
            return View();
        }
        /// <summary>
        /// 添加车位信息页面业务请求接口
        /// </summary>
        /// <param name="listDto">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddParkingPlace(ParkingLotListDto listDto)
        {
            IParkingManager manager = new ParkingManager();
            manager.AddOneParkingPlace(listDto);
            return Json(new { code = 0, msg = "新增成功" });
        }
        /// <summary>
        /// 车辆信息管理页面table删除业务请求接口
        /// </summary>
        /// <param name="id">车位信息ID</param>
        /// <returns></returns>
        public JsonResult DelParkingPlace(int id)
        {
            IParkingManager manager = new ParkingManager();
            manager.DelOneParkingPlaceById(id);
            return Json(new { code = 0, msg = "删除成功" }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 车位信息修改页面
        /// </summary>
        /// <param name="id">车位信息ID</param>
        /// <returns></returns>
        public ActionResult EditParkingPlace(int id)
        {
            IParkingManager manager = new ParkingManager();
            var data = manager.GetOneParkingPlaceById(id);
            return View(data);
        }
        /// <summary>
        /// 车位信息修改页面业务请求接口
        /// </summary>
        /// <param name="listDto">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditParkingPlace(ParkingLotListDto listDto)
        {
            IParkingManager manager = new ParkingManager();
            manager.EditOneParkingPlaceByDto(listDto);
            return Json(new { code = 0, msg = "修改成功" });
        }

        /// <summary>
        /// 招租信息管理页面方法
        /// </summary>
        /// <returns></returns>
        public ActionResult RentInfoList()
        {
            IParkingManager manager = new ParkingManager();
            var data = manager.GetParkingSpaceNumberLists();
            ViewBag.data = data;
            return View();
        }
        /// <summary>
        /// 招租信息管理页面table数据获取接口
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="Id">车位ID</param>
        /// <param name="parkinglLocation">车位位置</param>
        /// <param name="salesNature">销售状态</param>
        /// <returns></returns>
        public JsonResult GetRentInfoList(int limit, int page, string Id, string parkinglLocation, string salesNature)
        {
            IParkingManager manager = new ParkingManager();
            int datalet;
            var data = manager.getRentInfoListDtos(limit, page, out datalet, Id, parkinglLocation, salesNature);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 车位销售信息添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddRentInfo()
        {
            IParkingManager manager = new ParkingManager();
            var data = manager.GetParkingSpaceNumberLists();
            ViewBag.data = data;
            return View();
        }
        /// <summary>
        /// 车位招租信息添加发布
        /// </summary>
        /// <param name="models">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddRentInfo(AddNewRentInfoViewModels models)
        {
            IParkingManager manager = new ParkingManager();
            GetRentInfoListDto dto = new GetRentInfoListDto()
            {
                parkingId = models.parkingId,
                salesNature = models.salesNature,
                salesAmount = models.salesAmount,
                remark = models.remark,
                salesPeople = Session["UserName"].ToString()
                //salesPeople = "卢俊宇"
            };
            manager.AddOneRentInfoByModel(dto);
            return Json(new { code = 0, msg = "添加成功" });
        }
        /// <summary>
        /// 车位招租信息页面table表格删除按钮
        /// </summary>
        /// <param name="id">车位ID</param>
        /// <returns></returns>
        public JsonResult DelRentInfo(int id)
        {
            IParkingManager manager = new ParkingManager();
            manager.DelOneRentInfoById(id);
            return Json(new { code = 0, msg = "删除成功" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 车位销售信息修改页面
        /// </summary>
        /// <param name="id">销售ID</param>
        /// <returns></returns>
        public ActionResult EditRentInfo(int id)
        {
            IParkingManager manager = new ParkingManager();
            var data = manager.GetOneRentInfoById(id);
            var list = manager.GetParkingSpaceNumberLists();
            ViewBag.data = list;
            return View(data);
        }
        /// <summary>
        /// 车位销售信息修改页面业务请求接口
        /// </summary>
        /// <param name="models">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditRentInfo(AddNewRentInfoViewModels models)
        {
            IParkingManager manager = new ParkingManager();
            GetRentInfoListDto dto = new GetRentInfoListDto()
            {
                Id = models.Id,
                parkingId = models.parkingId,
                salesNature = models.salesNature,
                salesAmount = models.salesAmount,
                remark = models.remark,
            };
            manager.EditOneRentInfoByDto(dto);
            return Json(new { code = 0, msg = "修改成功" });
        }

    }
}